<template>
  <!--  比价单 列表  -->
  <div class="PriceComparativeList">
    <div class="body">
      <div class="g-con">
        <div class="main">
          <div class="title">{{ t('userCenter.t17') }}</div>

          <el-tabs
            v-model="activeName"
            type="card"
            class="mt-2"
            @tab-change="tabChange"
          >
            <el-tab-pane
              :label="item.label"
              :name="item.name"
              :key="item.name"
              v-for="item in tabsData"
            >
            </el-tab-pane>
          </el-tabs>

          <!-- 坞修比价单 -->
          <template v-if="activeName === 'dockRepair'">
            <div class="search flexsc">
              <div class="item">
                <el-input
                  v-model="searchData.orderNo"
                  :placeholder="t('invitationQuotation.p1')"
                />
              </div>
              <div class="item">
                <el-select-v2
                  v-model="searchData.shippingId"
                  :options="shipOptions"
                  :placeholder="t('invitationQuotation.p2')"
                  clearable
                  filterable
                />
              </div>
              <el-button
                type="primary"
                @click="handleSearch"
              >
                {{ t('search') }}
              </el-button>
              <!-- <el-button
                type="primary"
                color="#252628"
                @click="handleReset"
              >
                {{ t('invitationQuotation.b2') }}
              </el-button> -->
            </div>
            <div class="g-order-list">
              <div
                class="li mt-2"
                v-for="item in listData"
                :key="item.id"
              >
                <div class="attr">
                  <div class="item">
                    <span>{{ t('invitationQuotation.attr1') }}:</span>
                    <span>{{ item.orderNo }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('invitationQuotation.attr2') }}:</span>
                    <span>{{ item.orderName }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('userCenter.pcattr1') }}:</span>
                    <span>{{ dateFormat(item.createTime) }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('userCenter.pcattr2') }}:</span>
                    <span>{{ item.compareCount }}</span>
                  </div>
                </div>

                <div class="bottom flexsb mt-3">
                  <div class="">
                    <el-button
                      type="primary"
                      @click="open(item, '/user/dockOrderPriceComparative')"
                    >
                      {{ t('invitationQuotation.b3') }}
                    </el-button>
                    <el-button
                      v-if="isCreator(item)"
                      type="primary"
                      @click="handleConfigPerson(item)"
                    >
                      {{ t('userCenter.b11') }}
                    </el-button>
                  </div>
                </div>
              </div>
            </div>
          </template>

          <!-- 采购比价单 -->
          <template v-if="activeName === 'procurement'">
            <div class="search flexsc">
              <div class="item">
                <el-input
                  v-model="searchData.orderNo"
                  placeholder="采购单编号"
                />
              </div>
              <div class="item">
                <el-select-v2
                  v-model="searchData.shippingId"
                  :options="shipOptions"
                  :placeholder="t('invitationQuotation.p2')"
                  clearable
                  filterable
                />
              </div>
              <el-button
                type="primary"
                @click="handleSearch"
              >
                {{ t('invitationQuotation.b1') }}
              </el-button>
              <el-button
                type="primary"
                color="#252628"
                @click="handleReset"
              >
                {{ t('invitationQuotation.b2') }}
              </el-button>
            </div>
            <div class="g-order-list">
              <div
                class="li mt-2"
                v-for="item in listData"
                :key="item.id"
              >
                <div class="attr">
                  <div class="item">
                    <span>{{ t('invitationQuotation.attr1_2') }}:</span>
                    <span>{{ item.orderNo }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('invitationQuotation.attr2_2') }}:</span>
                    <span>{{ item.orderName }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('userCenter.pcattr1') }}:</span>
                    <span>{{ dateFormat(item.createTime) }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('userCenter.pcattr2') }}:</span>
                    <span>{{ item.compareCount }}</span>
                  </div>
                </div>

                <div class="bottom flexsb mt-3">
                  <div class="">
                    <el-button
                      type="primary"
                      @click="open(item, '/user/procurementPriceComparative')"
                    >
                      {{ t('invitationQuotation.b3') }}
                    </el-button>
                    <el-button
                      v-if="isCreator(item)"
                      type="primary"
                      @click="handleConfigPerson(item)"
                    >
                      {{ t('userCenter.b11') }}
                    </el-button>
                  </div>
                </div>
              </div>
            </div>
          </template>

          <!-- 外协比价单 -->
          <template v-if="activeName === 'externalAssistance'">
            <div class="search flexsc">
              <div class="item">
                <el-input
                  v-model="searchData.orderNo"
                  :placeholder="t('invitationQuotation.attr1_3')"
                />
              </div>
              <div class="item">
                <el-select-v2
                  v-model="searchData.shippingId"
                  :options="shipOptions"
                  :placeholder="t('invitationQuotation.p2')"
                  clearable
                  filterable
                />
              </div>
              <el-button
                type="primary"
                @click="handleSearch"
              >
                {{ t('invitationQuotation.b1') }}
              </el-button>
              <el-button
                type="primary"
                color="#252628"
                @click="handleReset"
              >
                {{ t('invitationQuotation.b2') }}
              </el-button>
            </div>
            <div class="g-order-list">
              <div
                class="li mt-2"
                v-for="item in listData"
                :key="item.id"
              >
                <div class="attr">
                  <div class="item">
                    <span>{{ t('invitationQuotation.attr1_3') }}:</span>
                    <span>{{ item.orderNo }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('invitationQuotation.attr2_3') }}:</span>
                    <span>{{ item.orderName }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('userCenter.pcattr1') }}:</span>
                    <span>{{ dateFormat(item.createTime) }}</span>
                  </div>
                  <div class="item">
                    <span>{{ t('userCenter.pcattr2') }}:</span>
                    <span>{{ item.compareCount }}</span>
                  </div>
                </div>

                <div class="bottom flexsb mt-3">
                  <div class="">
                    <el-button
                      type="primary"
                      @click="open(item, '/user/externalAssistancePriceComparative')"
                    >
                      {{ t('invitationQuotation.b3') }}
                    </el-button>
                    <el-button
                      v-if="isCreator(item)"
                      type="primary"
                      @click="handleConfigPerson(item)"
                    >
                      {{ t('userCenter.b11') }}
                    </el-button>
                  </div>
                </div>
              </div>
            </div>
          </template>

          <div class="flexcc mt-3">
            <el-pagination
              layout="prev, pager, next"
              :hide-on-single-page="total <= page.pageSize"
              :total="total"
              v-model:current-page="page.pageNum"
              :page-size="page.pageSize"
              @current-change="handleCurrentChange"
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </div>

    <!-- 设置可查看人员  -->
    <ConfigPerson
      v-if="dialogConfigPerson"
      v-model="dialogConfigPerson"
      :editId="editId"
      :title="t('userCenter.b11')"
      objTable="shcw_compare_price"
    />
  </div>
</template>

<script setup>
import { ref, reactive, onBeforeMount, watch, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { shippingListByEnterpriseIdApi } from '@/service/shipping'
import { useI18n } from 'vue-i18n'
const { t, locale } = useI18n()
import { dateFormat } from '@/utils/index'
import { selectShcwComparePriceListApi } from '@/service/common.js'
import useLogin from '@/stores/login'
import ConfigPerson from '@/views/userCenter/ConfigPerson.vue' // 设置可查看人员

const router = useRouter()
const route = useRoute()
const storeLogin = useLogin()

const type = route.query.type

watch(locale, () => {
  init()
})

onBeforeMount(() => {
  init()
})

// tab
const tabsData = ref([])
const setTabsData = () => {
  const arr = [
    {
      label: t('userCenter.pl1'),
      name: 'dockRepair',
      role: ['05', '06'] // 船东/船管
    },
    {
      label: t('userCenter.pl2'),
      name: 'procurement',
      role: ['05', '06', '07'] // 船东/船管/修船厂
    },
    {
      label: t('userCenter.pl3'),
      name: 'externalAssistance',
      role: ['05', '06', '07'] // 船东/船管/修船厂
    }
  ]

  tabsData.value = arr.filter((item) => item.role.includes(storeLogin.userInfo.customerClassCode))
  activeName.value = type || tabsData.value[0].name
}
const activeName = ref('dockRepair')
const tabChange = (value) => {
  page.value.pageNum = 1
  searchData.value = {}
  listData.value = []
  getList()
}

// 查询
const searchData = ref({})
const shipOptions = ref([])
const getShippingList = () => {
  shippingListByEnterpriseIdApi().then(({ rows }) => {
    shipOptions.value = rows.map((r) => {
      return {
        value: r.id,
        label: r.shippingName
      }
    })
  })
}
const handleSearch = () => {
  page.value.pageNum = 1
  getList()
}
const handleReset = () => {
  page.value.pageNum = 1
  searchData.value = {}
  getList()
}

// 列表
const listData = ref([])
const page = ref({
  pageNum: 1, // 当前页码
  pageSize: 6 // 每页条数
})
const total = ref(0) // 总数
const getList = () => {
  const obj = {
    dockRepair: 'shcw_dock_order',
    procurement: 'shcw_procure_order',
    externalAssistance: 'shcw_assist_order'
  }
  selectShcwComparePriceListApi(page.value, {
    ...searchData.value,
    orderObjApi: obj[activeName.value]
  }).then(({ data: rows, total: t }) => {
    listData.value = rows
    total.value = t
  })
}
// 分页
const handleCurrentChange = (val) => {
  getList()
}

// 查看
const open = (item, path) => {
  const url = router.resolve({
    path,
    query: {
      id: item.id,
      type: 'detail'
    }
  })
  window.open(url.href)
}

// 授权查看人
const editId = ref(null)
const dialogConfigPerson = ref(false)
const handleConfigPerson = ({ id }) => {
  dialogConfigPerson.value = true
  editId.value = id
}
const isCreator = ({ createBy }) => {
  return createBy === storeLogin.userInfo.id
}

//
const init = async () => {
  setTabsData()
  getShippingList()
  handleSearch()
}
</script>

<style scoped lang="scss">
.PriceComparativeList {
  .el-button {
    min-width: 140px;
  }
  .body {
    padding: 30px 0 70px;
    background: #f5f5f5;
    margin-bottom: -76px;

    .main {
      background: #fff;
      padding: 25px 35px;
      .title {
        border-bottom: 2px solid #212121;
        padding-bottom: 20px;
        text-transform: uppercase;
        font-weight: bold;
        font-size: 20px;
        color: var(--mainColor);
      }
      .search {
        margin-top: 25px;
        .item {
          width: 200px;
          margin-right: 20px;
        }
      }
    }
  }
}
</style>
